﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Unity;
using Newtonsoft.Json.Linq;
using Services;
using ViewModels;

namespace Commands {
    public class QueryAllDataCommand : CommandBase {
        private IXContainer _hook;
        private IDisplayTable _IDisplayTable;

        public override void OnCreate(object hook) {
            base.OnCreate(hook);
            _hook = hook as IXContainer;

            _IDisplayTable = _hook.Resolve<IDisplayTable>();
        }
        public override void OnClick(object parameter) {
            try {
            }
            catch (Exception ex) {
                CommonHelp.LogHelper.Error(ex);
            }
        }

        public void AfterGetData(string responseString) {
            try {
                if (responseString == null) {
                    System.Windows.MessageBox.Show("获取数据失败，请稍后重试。");
                    return;
                }
                List<object> showDatas = new List<object>();
                JArray jArr = JArray.Parse(responseString);
                for (int i = 0; i < jArr.Count; i++) {
                    JToken jt = jArr[i];
                    var shps = new List<object>();
                    JArray shpsArr = jt["shps"] as JArray;
                    foreach (var item in shpsArr) {
                        shps.Add(new {
                            si = item["sn"],
                            al = item["al"],
                            cr = item["cr"],
                            mm = item["mm"],
                            st = item["st"],
                            ts = item["ts"],
                            rn = item["rn"],
                            mf = item["mf"],
                            vf = item["vf"],
                        });
                    }

                    var pNode = new {
                        cn = jt["cn"],
                        cr = jt["cr"],
                        ci = jt["ci"],
                        ts = jt["ts"],
                        rn = jt["rn"],
                        mf = jt["mf"],
                        als = jt["als"],
                        ale = jt["ale"],
                        em = jt["em"],
                        tn = jt["tn"],
                        share = jt["share"],
                        SubData = shps,
                    };

                    showDatas.Add(pNode);
                }
                //_IDisplayTable.DisplayData = showDatas;

                _hook.Resolve<ITextHint>().AddText(DateTime.Now.ToString() + " 获取到 " + showDatas.Count.ToString() + " 条 AIS 数据。");
            }
            catch (Exception ex) {
                CommonHelp.LogHelper.Error(ex);
                throw ex;
            }
            finally {
                this.IsEnabled = true;
            }
        }
    }
}
